package com.example.petplanet.config;


import com.example.petplanet.intercept.WebSocketHandshakeInterceptor;
import com.example.petplanet.util.JWTUtil;
import com.example.petplanet.util.SecureEncryptionUtil;
import io.jsonwebtoken.Claims;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
import jakarta.websocket.server.ServerEndpointConfig.Configurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.util.List;
import java.util.Map;

@Component
@Configuration
public class WebSocketConfigurator extends Configurator implements ServletContextInitializer {

    private static WebSocketHandshakeInterceptor interceptor;

    @Autowired
    public void setInterceptor(WebSocketHandshakeInterceptor interceptor) {
        WebSocketConfigurator.interceptor = interceptor;
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        interceptor.handleHandshake(sec, request, response);
    }

    @Override
    public void onStartup(ServletContext servletContext){
        // 设置文本消息缓冲区大小为10mb
        servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize", "10485760");
        // 设置二进制消息缓冲区大小为10mb
        servletContext.setInitParameter("org.apache.tomcat.websocket.binaryBufferSize", "10485760");
    }

}